<?php
/**
 * FlickrPhotosetsConfig class file
 * @package FlickrPhotosets
 */

/**
 * This class handles the configuration page.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 8.4.30
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */
class FlickrPhotosetsConfig extends FlickrPhotosetsCanvas {
	
	/**
	 * The messages to print out after the header.
	 * @var string
	 */
	private $_messages;
	
	/**
	 * The level of Flickr permissions.
	 * @var string
	 */
	public $perms;
	
	public $sm = 1;
	public $em = 1;
	
	/**
	 * Requires that the page is in the frame and that the user has
	 * added the application. Initiates the functions that handle
	 * updates of the configuration.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct() {
		parent::__construct(FlickrPhotosetsCanvas::require_login, false, false, false);
		
		// Now that we have the UID, let's get their configuration data
		if (!$this->get_info()) {
			$this->error(FlickrPhotosetsErrors::$database_error);
		}

		// Migrate to new flickr api key
		$this->_migrate_flickr_key();
		
		// Set up Flickr
		$this->init_flickr(false, false);
		$this->flickr->setToken($this->config['flickr_token']);
		
		// If the user is authenticated
		if ($this->is_flickr_authorized()) {
			
			// Make sure we still have permissions on Flickr
			if (!FP_Auth::check_token($this->flickr)) {
				$this->facebook->redirect($this->canvas_url.'/auth.php?remove&skiprevoke');
			}
			
			// Get the user's info from Flickr
			$this->flickr_user = $this->flickr->people_getInfo($this->nsid);
			
			// Check for and update permissions
			$this->perms = FP_Auth::get_perms($this->flickr);
			if ($this->perms == 'none' and $this->config['flickr_auth'] != 'n') {
				$this->_db->set_flickr_auth($this->uid, 'n');
				$this->config['flickr_auth'] = 'n';
			}
			else if ($this->perms == 'read' and $this->config['flickr_auth'] != 'r') {
				$this->_db->set_flickr_auth($this->uid, 'r');
				$this->config['flickr_auth'] = 'r';
			}
			else if ($this->perms == 'write' and $this->config['flickr_auth'] != 'w') {
				$this->_db->set_flickr_auth($this->uid, 'w');
				$this->config['flickr_auth'] = 'w';
			}
			
		}
		
		// Set info
		$this->set_info_box();
		
		// Handle updates
		$this->handle_post();
		$this->handle_update();
	}
	
	/**
	 * Adds the FBML for an error message to the messages string.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $text The success message to add
	 */
	private function add_error_message($text) {
		$this->_messages .= '<div style="overflow: hidden; margin-bottom: -21px;" id="error'.$this->em.'"><p class="error">'.$text.'</p></div>';
		$this->_messages .= '<script type="text/javascript">Animation(document.getElementById(\'error'.$this->em.'\')).duration(2500).checkpoint().to(\'height\', \'0px\').to(\'opacity\', 0).to(\'margin-bottom\', 0).hide().ease(Animation.ease.end).go(); </script>';
		$this->em++;
	}
	
	/**
	 * Adds the FBML for a success message to the messages string.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $text The success message to add
	 */
	private function add_success_message($text) {
		$this->_messages .= '<div style="overflow: hidden; margin-bottom: -21px;" id="success'.$this->sm.'"><p class="message">'.$text.'</p></div>';
		$this->_messages .= '<script type="text/javascript">Animation(document.getElementById(\'success'.$this->sm.'\')).duration(2500).checkpoint().to(\'height\', \'0px\').to(\'opacity\', 0).to(\'margin-bottom\', 0).hide().ease(Animation.ease.end).go(); </script>';
		$this->sm++;
	}
	
	/**
	 * Uses the post variables to update the user's preferences.
	 * Redirects to other pages as necessary. Validates information
	 * as necessary.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com
	 */
	public function handle_post() {
		/*
		Update privacy settings
		*/
		if (isset($_POST['fp_privacy']) and $_POST['fp_privacy'] == 'Set') {
			$this->config['privacy_filter'] = $_POST['privacy_filter'];
			$this->config['privacy_visibility'] = $_POST['privacy_visibility'];
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->add_success_message('You have successfully updated your privacy options.');
			}
		}
		/*
		Reset privacy settings to defaults
		*/
		else if (isset($_POST['fp_privacy']) and $_POST['fp_privacy'] == 'Reset') {
			$this->config['privacy_filter'] = $this->privacy_filter['public'];
			$this->config['privacy_visibility'] = $this->privacy_visibility['all'];
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->add_success_message('You have successfully reset your privacy options to their default settings.');
			}
		}
		/*
		Update program features
		*/
		else if (isset($_POST['fp_features']) and $_POST['fp_features'] == 'Set') {
			$this->config['enable_gallery_photostream'] = $_POST['enable_gallery_photostream'];
			$this->config['enable_gallery_photosets'] = $_POST['enable_gallery_photosets'];
			$this->config['enable_gallery_geotagged'] = $_POST['enable_gallery_geotagged'];
			$this->config['enable_comments'] = $_POST['enable_comments'];
			// $this->config['enable_newsfeed'] = $_POST['enable_newsfeed'];
			// $this->config['enable_tagging'] = $_POST['enable_tagging'];
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->add_success_message('You have successfully enabled or disabled program features.');
			}
			
		}
		/*
		Reset features to default settings (enable all)
		*/
		else if (isset($_POST['fp_features']) and $_POST['fp_features'] == 'Reset') {
			$this->config['enable_gallery_photostream'] = 1;
			$this->config['enable_gallery_photosets'] = 1;
			$this->config['enable_gallery_geotagged'] = 1;
			$this->config['enable_comments'] = 1;
			//$this->config['enable_newsfeed'] = 1;
			//$this->config['enable_tagging'] = 0;
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->add_success_message('You have successfully reset the program features.');
			}
		}
		/*
		Update profile settings
		*/
		else if (isset($_POST['fp_profile']) and $_POST['fp_profile'] == 'Set') {
			$this->config['profile_display'] = $_POST['profile_display'];
			$this->config['profile_count'] = $_POST['profile_count'];
			$this->config['profile_tags'] = $_POST['profile_tags'];
			$this->config['profile_tagmode'] = $_POST['profile_tagmode'];
			$this->config['profile_sort'] = $_POST['profile_sort'];
			
			// Make sure the photo count is valid
			if ($this->config['profile_count'] > 30) {
				$this->config['profile_count'] = 30;
			}
			else if ($this->config['profile_count'] < 1) {
				$this->config['profile_count'] = 1;
			}
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->facebook->redirect($this->canvas_url.'/update/');
			}
		}
		/*
		Reset profile settings to defaults
		*/
		else if (isset($_POST['fp_profile']) and $_POST['fp_profile'] == 'Reset') {
			$this->config['profile_display'] = $this->profile_display['last'];
			$this->config['profile_count'] = $this->profile_count;
			$this->config['profile_tags'] = '';
			$this->config['profile_tagmode'] = $this->tagmode['any'];
			$this->config['profile_sort'] = $this->profile_sort['date_posted_desc'];
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->facebook->redirect($this->canvas_url.'/update/');
			}
		}
		/*
		Fix profile box issues
		*/
		else if (isset($_POST['fp_problems']) and $_POST['fp_problems'] == 'Reset profile box') {
			// Reset FBML
			$this->set_profile_fbml();
			
			// Redirect to the update page
			$this->facebook->redirect($this->canvas_url.'/update/');
		}
		
	}
	
	/**
	 * Handle get variables sent back from update.php.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function handle_update() {
		if (isset($_GET['profileupdated'])) {
			$this->add_success_message('You have successfully updated your profile options.');
		}
		else if (isset($_GET['profileremoved'])) {
			$this->add_success_message('You have successfully removed the profile feature.');
		}
	}
	
	/**
	 * Prints out the messages.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function print_messages() {
		echo $this->_messages;
	}
	
}

?>